package com.iflytek.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.iflytek.service.IStudentService;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 周超
 * @since 2025-05-29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/student")
public class StudentController {
    private final IStudentService studentService;

    @PostMapping("/face")
    public Map<String, Object> addStudentFace(@RequestParam String name, @RequestParam String className,
                                              @RequestPart("file") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        boolean addResult = studentService.addStudentFace(name, className, file);
        if (addResult) {
            result.put("code", 200);
            result.put("message", "添加学生人脸信息成功");
        } else {
            result.put("code", 500);
            result.put("message", "添加学生人脸信息失败");
        }
        return result;
    }

    @PostMapping("/import")
    public Map<String, Object> importStudents(@RequestPart("file") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = studentService.importStudentInfo(file);
            result.put("code", 200);
            result.put("message", "导入成功");
            result.put("data", count);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "导入失败: " + e.getMessage());
        }
        return result;
    }
}
